package study.springboot.starter.redis.redisson;

import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.net.URL;

@Slf4j
@Configuration
public class RedissonXAutoConfiguration {

    private static final String CFG_FILE = "redisson-cfg.yml";

    @Bean
    @ConditionalOnResource(resources = "/redisson-cfg.yml")
    public RedissonClient redissonClient() throws IOException {
        log.info(">>>>>> create RedissonClient");
        URL url = RedissonXAutoConfiguration.class.getClassLoader()
                .getResource(CFG_FILE);
        Config config = Config.fromYAML(url);
        return Redisson.create(config);
    }

    @Bean
    @ConditionalOnBean({RedissonClient.class})
    public RedissonXClient redissonXClient(RedissonClient redissonClient) {
        log.info(">>>>>> create RedissonXClient");
        return new RedissonXClient(redissonClient);
    }
}
